package com.lysj.merchant.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.crypto.digest.BCrypt;
import com.lysj.admin.utils.annotation.SystemLog;
import com.lysj.admin.utils.annotation.UserId;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.validation.Validation;
import com.lysj.admin.utils.validation.annotation.Valid;
import com.lysj.admin.utils.validation.domain.BindingResult;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.merchant.management.domain.Role;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.domain.UserRole;
import com.lysj.merchant.management.service.RoleService;
import com.lysj.merchant.management.service.UserRoleService;
import com.lysj.merchant.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zk
 * @description 用户表控制层
 * @create 2018-07-25 15:02:19
 **/
@Slf4j
@RestController
@RequestMapping("/merchant/user")
public class UserController extends BaseController<User> {

    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    @Override
    public UserService getService() {
        return userService;
    }

    /**
     * @author zk
     * @date 2018-08-13 21:03
     * @Description 查询用户
     */
    @Override
    public Resp list(User entity, PageVo pageVo) {
        Page<User> page = getService().list(entity, pageVo);
        for (User user : page.getContent()) {
            user.setRoles(userRoleService.findUserRoleByUserId(user.getId()));
        }
        return new Resp().success(page);
    }

    /**
     * @author Created by wtl on 2019/3/14 15:56
     * @Description 店长添加员工
     */
    @PostMapping("/add")
    @SystemLog(description = "店长添加员工")
    public Resp add(@Valid User entity, String[] roleIds) {
        return new Resp().success(userService.add(entity, roleIds));
    }

    /**
     * @author zk
     * @date 2018-08-13 21:08
     * @Description 修改用户
     */
    @PostMapping("/update_user")
    @SystemLog(description = "修改用户")
    public Resp update(User entity, String[] roleIds) {
        User user = userService.findOne(entity.getId());
        if (user == null) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此用户");
        }
        //获取Hutool拷贝实例
        CopyOptions copyOptions = CopyOptions.create();
        //忽略为null值得属性
        copyOptions.setIgnoreNullValue(true);
        //进行属性拷贝
        BeanUtil.copyProperties(entity, user, copyOptions);
        Set<String> ignoreProperties = new HashSet<>();
        ignoreProperties.add("username");
        ignoreProperties.add("password");
        BindingResult bindingResult = Validation.valid(user, ignoreProperties);
        if (!bindingResult.isFlag()) {
            return new Resp().error(Resp.Status.PARAM_ERROR, bindingResult.getMessage().get(0));
        }

        // 先删除原来的所有权限
        userRoleService.deleteByUserId(user.getId());
        // 员工类型
        int userType = 3;
        for (String s : roleIds) {
            Role role = roleService.findOne(s);
            if (Role.RoleType.MANAGER.getCode().equals(role.getRoleType())) {
                userType = 2;
                break;
            }
        }
        user.setUserType(userType);
        userService.save(user);

        for (String s : roleIds) {
            if (!roleService.countRoleId(s)) {
                throw new RuntimeException("角色信息有误");
            }
            UserRole ur = new UserRole();
            ur.setRoleId(s);
            ur.setUserId(entity.getId());
            userRoleService.save(ur);
        }
        return new Resp().success("修改成功");

    }

    /**
     * @author Created by wtl on 2019/3/13 16:56
     * @Description 商户下拉框，添加店长时需要
     */
    @Override
    public Resp selectItem() {
        return new Resp().success(userService.selectItems());
    }

    /**
     * @author zk
     * @date 2018-08-13 20:41
     * @Description 修改密码
     */
    @PostMapping("/modify_password")
    @SystemLog(description = "修改密码")
    public Resp modifyPassword(@UserId String id,  String password, String newPassword) {
        return new Resp().success(userService.modifyPassword(id, password, newPassword));
    }

    /**
     * @author zk
     * @date 2018-08-13 21:00
     * @Description 禁用用户
     */
    @PostMapping("/disable")
    public Resp disable(String id) {
        User user = userService.findOne(id);
        if (user == null) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此用户");
        }
        if (user.getDelFlag().equals(CommonConstant.DEL_FLAG)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户已被禁用");
        }
        user.setDelFlag(CommonConstant.DEL_FLAG);
        userService.save(user);
        return new Resp().success("禁用成功");
    }

    /**
     * @author zk
     * @date 2018-08-13 21:00
     * @Description 启用用户
     */
    @PostMapping("/enable")
    public Resp enable(String id) {
        User user = userService.findOne(id);
        if (user == null) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此用户");
        }
        if (user.getDelFlag().equals(CommonConstant.NORMAL_FLAG)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "该用户已启用");
        }
        user.setDelFlag(CommonConstant.NORMAL_FLAG);
        userService.save(user);
        return new Resp().success("启用成功");
    }

    @PostMapping("/reset_password")
    @SystemLog(description = "重置密码")
    public Resp resetPassword(String id) {
        User user = userService.findOne(id);
        if (user == null) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此用户");
        }
        user.setPassword(BCrypt.hashpw("123456"));
        userService.save(user);
        return new Resp().success("密码已重置为：123456");
    }

    @Override
    public Resp delete(String[] ids) {
        return null;
    }

    @Override
    public Resp logicalDel(String[] ids) {
        return null;
    }
}
